package com.xjrsoft.module.form.util;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.BooleanUtil;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.SpringUtil;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.core.tool.utils.WebUtil;
import com.xjrsoft.module.base.entity.XjrBaseModule;
import com.xjrsoft.module.base.service.IXjrBaseModuleService;

public final class FormAuthUtil {

    public static final String KEY_OF_PERMISSION_ADD = "add";

    public static final String KEY_OF_PERMISSION_VIEW = "view";

    public static final String KEY_OF_PERMISSION_UPDATE = "modify";

    public static final String KEY_OF_PERMISSION_DELETE = "delete";

    private static final IXjrBaseModuleService moduleService;

    static {
        moduleService = SpringUtil.getBean(IXjrBaseModuleService.class);
    }

    private FormAuthUtil(){}

    public static void checkFormAuth(String moduleId, String permission) {
        String isWorkFlow = WebUtil.getRequest().getHeader("isWorkFlow");
        if (BooleanUtil.toBoolean(isWorkFlow) || StringUtil.equalsIgnoreCase(StpUtil.getLoginDevice(), "app") || SecureUtil.isAdminUser()) {
            return;
        }
        XjrBaseModule module = moduleService.getById(moduleId);
        if (module != null) {
            StpUtil.checkPermission(module.getEnCode() + ":" + permission);
        }
    }

    public static void checkCurrentFormAuth(String permission) {
        checkFormAuth(SecureUtil.getModuleId(), permission);
    }
}
